<?php

namespace App\Modules\User\Models;

use App\Library\OSS;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * Class User
 * @package App\Modules\Models
 * @property string $nickname
 * @property string $avatar
 * @property string $mobile
 * @property string $openid
 * @property string $unionid
 * @property string $official_account_openid
 * @property string $session_key
 * @property integer $referrer_id
 * @property integer $bind_id
 * @property integer $level
 * @property boolean $has_order
 * @property integer $personal_id
 * @property string $invite_code
 * @property int $share
 * @property int $avatar_key
 * @property string $employee_identity
 * @property string $position
 * @property integer $shop_id
 * @property string $last_time
 */
class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    public static $levelShow = ["注册用户", "返利会员", "城市合伙人", "商家职员"];

    const EMPLOYEE_IDENTITY_PRIMARY = '商家职员';
    const EMPLOYEE_IDENTITY_MIDDEL = '商家管理';
    const EMPLOYEE_IDENTITY_SENIOR = '平台管理';

    protected $fillable = [
        'nickname',
        'avatar',
        'mobile',
        'openid',
        'unionid',
        'official_account_openid',
        'session_key',
        'referrer_id',
        'bind_id',
        'level',
        'has_order',
        'personal_id',
        'invite_code',
        'share',
        'avatar_key',
        'employee_identity',
        'position',
        'shop_id',
        'last_time',
        'name',
        'gender',
        'is_palm_management'//是否开通掌上管理
    ];

    protected $casts = [
        'shop_id' => 'array',
    ];

    /**
     * @return Account
     */
    public function getAccount()
    {
        /** @var Account $account */
        $account = Account::query()->where('id', $this->id)->first();
        if (is_null($account)) {
            $account = Account::query()->create([
                'id' => $this->id,
                'income' => 0,
                'money' => 0,
                'outlay' => 0,
            ]);
        }
        return $account;
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * @return User
     */
    public function getTeam()
    {
        return Cache::remember("user:team:{$this->id}", 2, function () {
            return User::query()
                ->where('personal_id', $this->id)
                ->where('level', 2)
                ->get();
        });
    }
}
